<?php
class area extends spController
{

	function index(){ // 区块管理
		$this->tpl_title = "区块管理";
		$this->areas = spClass("lib_contents")->spPager($this->spArgs('page', 1), 10)->findAll(array("type" => 'area'),"cid DESC");  
		$this->pager = spClass("lib_contents")->spPager()->getPager();
		$this->display("admin/area_index.html");
	}

	function add(){ // 添加区块
		$user = spClass("lib_contents");
		$newrow = array( // 这里制作新增记录的值
			'title' => $this->spArgs('title'),
			'overview' => $this->spArgs('overview'),			
			'text' => $this->spArgs('text'),
			'created' => mktime(),			
			'uid' => $_SESSION["userinfo"]["uid"],
			'type' => 'area',
		);
		$user->create($newrow); 
		$this->success("区块撰写成功！", spUrl("area", "index"));
	}

	function edit(){ // 编辑区块
		$this->tpl_title = "编辑区块";
		$this->formUrl = spUrl("area", "update");
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 查找表，获取信息
			$this->info = spClass("lib_contents")->find(array('cid'=>$cid));
			$this->display("admin/area_edit.html");
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("area","index"));
		}
	}
	
	function write(){ // 新建区块
		$this->tpl_title = "新建区块";
		$this->formUrl = spUrl("area", "add");
		$this->display("admin/area_write.html");
	}
	
	function update(){ //更新区块
		$post = spClass("lib_contents");
		if( $cid = $this->spArgs("cid") ){
		$conditions = array("cid"=>$cid); 
		$newrow = array(
			'title' => $this->spArgs('title'), 
			'modified' => mktime(),
			'text' => $this->spArgs('text'),
			'overview' => $this->spArgs('overview'),
			'uid' => $_SESSION["userinfo"]["uid"],
			);
		$post->update($conditions, $newrow); // 更新记录
		$this->success("更新区块成功！", spUrl("area", "index"));
		}
	}

	function del(){// 删除区块
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 执行删除
			spClass("lib_contents")->delete(array('cid'=>$cid));
			$this->success("删除成功！", spUrl("area","index"));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("area","index"));
		}
	}
	
}